Add Event#location= and make location an accessible attribute.

Add long aliases for lat and lng to Location.

Akinori MUSHA 10 lat temu
rodzic
commit
1406f37d48
3 zmienionych plików z 24 dodań i 3 usunięć
  1. 4 2
      app/models/agents/user_location_agent.rb
  2. 14 1
      app/models/event.rb
  3. 6 0
      lib/location.rb

+ 4 - 2
app/models/agents/user_location_agent.rb

@@ -65,8 +65,10 @@ module Agents
65 65
     private
66 66
 
67 67
     def handle_payload(payload)
68
-      if payload[:latitude].present? && payload[:longitude].present?
69
-        create_event payload: payload, lat: payload[:latitude].to_f, lng: payload[:longitude].to_f
68
+      location = Location.new(payload)
69
+
70
+      if location.present?
71
+        create_event payload: payload, location: location
70 72
       end
71 73
     end
72 74
   end

+ 14 - 1
app/models/event.rb

@@ -7,7 +7,7 @@ class Event < ActiveRecord::Base
7 7
   include JSONSerializedField
8 8
   include LiquidDroppable
9 9
 
10
-  attr_accessible :lat, :lng, :payload, :user_id, :user, :expires_at
10
+  attr_accessible :lat, :lng, :location, :payload, :user_id, :user, :expires_at
11 11
 
12 12
   acts_as_mappable
13 13
 
@@ -53,6 +53,19 @@ class Event < ActiveRecord::Base
53 53
       speed: payload[:speed].presence)
54 54
   end
55 55
 
56
+  def location=(location)
57
+    case location
58
+    when nil
59
+      self.lat = self.lng = nil
60
+      return
61
+    when Location
62
+    else
63
+      location = Location.new(location)
64
+    end
65
+    self.lat, self.lng = location.lat, location.lng
66
+    location
67
+  end
68
+
56 69
   # Emit this event again, as a new Event.
57 70
   def reemit!
58 71
     agent.create_event :payload => payload, :lat => lat, :lng => lng

+ 6 - 0
lib/location.rb

@@ -34,6 +34,9 @@ class Location
34 34
     }
35 35
   end
36 36
 
37
+  alias latitude  lat
38
+  alias latitude= lat=
39
+
37 40
   def lng=(value)
38 41
     self[:lng] = floatify(value) { |f|
39 42
       if f.abs <= 180
@@ -44,6 +47,9 @@ class Location
44 47
     }
45 48
   end
46 49
 
50
+  alias longitude  lng
51
+  alias longitude= lng=
52
+
47 53
   def radius=(value)
48 54
     self[:radius] = floatify(value) { |f| f if f >= 0 }
49 55
   end